#region Using Statements

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion

namespace _1942
{
    /// <summary>
    /// This game component implements a manager for all hunter planes in the game.
    /// </summary>
    public class HunterManager : DrawableGameComponent
    {
        #region Variables

        // List of active planes
        protected List<Hunter> hunters;
        
        // Constant for initialplane count
        private const int STARTPLANECOUNT = 0;

        // Time for a new plane
        private const int ADDHUNTERTIME = 35000;

        // Audio
        private AudioLibrary audio;

        protected Texture2D planeTexture;
        protected TimeSpan elapsedTime = TimeSpan.Zero;

        private Color[] texturesData;
			 
        private bool shouldSpawn = true;

        #endregion

        #region Constructors

        public HunterManager(Game game, ref Texture2D theTexture)
            : base(game)
        {
            planeTexture = theTexture;
            
            hunters = new List<Hunter>();

            // Get the audio library
            audio = (AudioLibrary)Game.Services.GetService(typeof(AudioLibrary));

            Rectangle frame = new Rectangle();
            frame.X = 268;
            frame.Y = 70;
            frame.Width = 32;
            frame.Height = 31;

            texturesData = new Color[frame.Width * frame.Height];
            theTexture.GetData<Color>(0, frame, texturesData, 0, frame.Width * frame.Height);

        }


        #endregion

        #region Methods

        /// <summary>
        /// Allows the game component to perform any initialization it needs to 
        /// before starting to run.  This is where it can query for any required
        /// services and load content.
        /// </summary>
        public override void Initialize()
        {
            hunters.Clear();

            Start();

            for (int i = 0; i < STARTPLANECOUNT; i++)
            {
                hunters[i].Initialize();
            }

            base.Initialize();
        }

        /// <summary>
        /// Start the hunter attack
        /// </summary>
        public void Start()
        {
            // Initialize a counter
            elapsedTime = TimeSpan.Zero;

            // Add the planes
            for (int i = 0; i < STARTPLANECOUNT; i++)
            {
                AddNewHunter();
            }
        }

        /// <summary>
        /// Check if is a moment for a new hunter
        /// </summary>
        private void CheckforNewHunter(GameTime gameTime)
        {
            elapsedTime += gameTime.ElapsedGameTime;

            if (elapsedTime > TimeSpan.FromMilliseconds(ADDHUNTERTIME))
            {
                elapsedTime -= TimeSpan.FromMilliseconds(ADDHUNTERTIME);
                AddNewHunter();
            }
        }

        /// <summary>
        /// Add a new plane in the scene
        /// </summary>
        private Hunter AddNewHunter()
        {
            Hunter newHunter = new Hunter(Game, ref planeTexture);
            newHunter.Initialize();

            hunters.Add(newHunter);

            return newHunter;
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if(shouldSpawn) CheckforNewHunter(gameTime);

            // Update Planes
            foreach (Hunter item in hunters)
            {
                if (item.Enabled)
                    item.Update(gameTime);
            }

            hunters.RemoveAll(delegate(Hunter item)
            {
                if (!item.Enabled)
                    return true;
                return false;
            }
            );

            base.Update(gameTime);
        }

        /// <summary>
        /// Allows the game component draw your content in game screen
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            // Draw the planes
            foreach (Hunter item in hunters)
            {
                if (item.Visible)
                    item.Draw(gameTime);
            }

            base.Draw(gameTime);
        }

        #endregion


        #region Properties

        public Color[] TextureData
        {
            get { return texturesData; }
        }

        /// <summary>
        /// All hunters in the game
        /// </summary>
        public List<Hunter> AllHunters
        {
            get { return hunters; }
        }
				   
        public bool ShouldSpawn
        {
            get { return shouldSpawn; }
            set { 
                shouldSpawn = value;
                foreach (Hunter h in hunters)
                {
                    h.ShouldSpawn = false;
                }
            }
        }
        #endregion 
    }
}